package com.py.credit.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.GetRoleService;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;
@Primary
@Service
public class GetRoleServiceImpl implements GetRoleService {
	@Autowired
    private UrlHelper urlHelper;
	@Override
	public RestServiceResult<List<HashMap<String,Object>>> getRolesByUserId(Long id, String token) {
		 String url = urlHelper.getBaseServerUrl() + "/role/getRolesByUserId.json";
	        Map<String, String> params = new HashMap<>(1);
	        params.put("userId",String.valueOf(id));
	        String result = "";
	        try {
	            // 发送请求
	            result = HttpAgent.getInstance().sendHttpPost(url, params, token);
	            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<HashMap<String,Object>>>>() {
	            });
	        } catch (Exception e) {
	            RestServiceResult<List<HashMap<String, Object>>> userData = new RestServiceResult<>();
	            userData.setCode(RestServiceResult.CODE_UNKNOW);
	            userData.setMsg("系统异常，请稍后再试！");
	            ExceptionLogger.error(
	                    "远程服务-获取当前用户信息调用异常！(XypjRemoteBaseService#getRolesByUserId) URL:" + url + "Result:" + result,
	                    e.getMessage(), e);
	            return userData;
	        }
	}

}
